package com.zhijian.medical.service.impl;

import com.zhijian.medical.dao.local.RoutineStepMapper;
import com.zhijian.medical.entity.po.RoutineStep;
import com.zhijian.medical.entity.vo.request.RoutineStepSearchVo;
import com.zhijian.medical.entity.vo.request.RoutineStepSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.response.RoutineStepVo;
import com.zhijian.medical.service.RoutineStepService;
import com.zhijian.util.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.util.AssertUtil;
import org.springframework.beans.BeanUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 常规病理-流程状态业务处理层
 * 
 * @author auto
 * @version: 1.0
 * @date 2024-07-24 19:49:33
 */
@Slf4j
@Service
public class RoutineStepServiceImpl implements RoutineStepService {

	@Autowired
    private RoutineStepMapper mapper;

    @Override
    public RoutineStep selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public RoutineStepVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(RoutineStep routineStep) {
        Date now = new Date();
        routineStep.setCreateTime(now);
        routineStep.setUpdateTime(now);
        return mapper.insert(routineStep) > 0 ? routineStep.getId() : 0L;
    }

    @Override
    public Long insertSelective(RoutineStep routineStep) {
        Date now = new Date();
        routineStep.setCreateTime(now);
        routineStep.setUpdateTime(now);
        return mapper.insertSelective(routineStep) > 0 ? routineStep.getId() : 0L;
    }

    @Override
    public boolean update(RoutineStep routineStep) {
        Date now = new Date();
        routineStep.setUpdateTime(now);
        return mapper.updateByPrimaryKey(routineStep) != 0;
    }

    @Override
    public boolean updateStepByNums(String nums, String content, Byte step) {
        Integer count = this.count(RoutineStepSearchVo.builder().conventionPathologyNumber(nums).build());
        if (count == 1) {
            return mapper.updateStepByNums(nums, content, step) != 0;
        } else {
            return this.insert(RoutineStep.builder().conventionPathologyNumber(nums).step(step).content(content).build()) > 0;
        }

    }

    @Override
    public boolean updateByPrimaryKeySelective(RoutineStep record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(RoutineStepSaveOrUpdateVo saveOrUpdateVo) {
        RoutineStep routineStep = new RoutineStep();
        BeanUtils.copyProperties(saveOrUpdateVo, routineStep);
        return this.insert(routineStep) != 0;
    }

    @Override
    public boolean modify(RoutineStepSaveOrUpdateVo saveOrUpdateVo) {
        RoutineStep old = this.selectById(saveOrUpdateVo.getId());
        AssertUtil.notNull(old, "此数据不存在或者已被删除");
        RoutineStep routineStep = new RoutineStep();
        BeanUtils.copyProperties(saveOrUpdateVo, routineStep);
        return this.update(routineStep);
    }

    @Override
    public RoutineStepVo selectOne(RoutineStepSearchVo routineStepSearchVo) {
        return mapper.selectOne(routineStepSearchVo);
     }

    @Override
    public Integer count(RoutineStepSearchVo routineStepSearchVo) {
        return mapper.count(routineStepSearchVo);
    }

    @Override
    public List<RoutineStepVo> selectList(RoutineStepSearchVo routineStepSearchVo) {
        return mapper.selectList(routineStepSearchVo);
    }

    @Override
    public Page<RoutineStepVo> selectPage(RoutineStepSearchVo routineStepSearchVo) {
        AssertUtil.check(Objects.isNull(routineStepSearchVo.getPageNum()) || Objects.isNull(routineStepSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(routineStepSearchVo);
        //分页
        Page<RoutineStepVo> page = new Page<>(true, count, routineStepSearchVo.getPageNum(), routineStepSearchVo.getPageSize());
        routineStepSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<RoutineStepVo> responseVos = this.selectList(routineStepSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<RoutineStep> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(RoutineStepSearchVo routineStepSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(routineStepSearchVo);
    }
    
}